{literal}
    <style type="text/css">
		@import url('../../../static/admin/css/shippingtime.css');	
		#condition_list a.btn24{
			display: inline-table;
			margin-top: 8px;
		}
		
		button{
			font-size: 14pt;
			border: 1px solid black;
			padding: 6px 10px 5px 10px;
			cursor: pointer;
			color: #1575BF;
			background: none;
		}
		
		
		a.btn{
			border: 1px solid black;
			color: #1575BF;
			font-size: 14pt;
			padding: 6px;
			text-decoration: none;
		}
		a.btn:hover{			
			text-decoration: none;
			color: #1575BF;
		}
    </style>
{/literal}

{include file = "styles/header.tpl"}

<script type="text/javascript" src="{$smarty.const.HOST}/static/admin/js/ajaxfileupload.js"></script>
<div id="content" class="box">
    <h1>Create Rule</h1>	
<form method="post" id="frmRule" action="{$smarty.const.HOST_ADMIN}/shippingtime/createrule">
<table width="100%">
<tr>
	<td><label>Id: </label><input type="text" value="{$maxId}" disabled></input></td> 
	
</tr>

<tr>
	<td><label>Order: </label><input name="order" type="text" value="{$maxOrder + 1}"></input></td>	
</tr>

<tr>
	<td><label>Comment: </label><input type="text" name="comment" value="new rule"></input></td>	
</tr>

<tr>
	<td><label>Enabled: </label><input type="checkbox" name="enabled"></input></td>	
</tr>

<tr class="condition">
	<td>
		<label>Conditions: </label>		
		<div><a title="add a condition" class="btn24 add" href="#"><span></span></a></div>
		<div id="condition_list"  style="">
		</div>										
	</td>	
</tr>

<tr class="action">
	<td>
		<label>Action: </label> <br>	
		<div style="height: 60px">
			<div style="margin-left:0px;float: left;">
				<span>
					Type:
					<select id="action_type" name="action_type">
						<option value="RULE_ACTION_ADD_TIME">add time</option>
						<option value="RULE_ACTION_SET_TARGET_TIME">set target time</option>					
						<option value="RULE_ACTION_GET_LOCATION_TIME">add location time </option>					
					</select>
				</span>
				<span  class="action_value" id="action_value" style="margin-left:20px">
					Value:
					<input name="action_value"  value="{$rule.action_parsed.value}"></input>				
				</span>
				<span class="unit"></span>
				<span  class="action_offset" id='action_offset' style="margin-left:20px">
					Offset:
					<input name="action_offset" value="{$rule.action_parsed.offset}"></input>				
				</span>
				<span class="unit">(h)</span>
			</div>
			<div style="margin-left:20px;width:44%;float:left">			
				<span>
					<p style="margin-bottom:0px;">
						<input type="checkbox" id="action_rule_stop" name="action_rule_stop" ></input>
						Stop processing rule
					</p>
					
					<p id="action_text_display" style="margin-bottom:0px; display:none"> 
						Text display: 
						<input  name="action_text_display" style="width:80%" value="{$rule.action_parsed.text_display}"></input>
					</p>
				</span>
			</div>	
		</div>
	</td>
</tr>
</table>
<div align=left style="margin-top:25px">
	<span>
		<button class="btn" type="submit" name="action" value="save">Save</button>	
		<button class="btn" type="submit"  name ="action" value="save_close">Save & Close</button>	
		<button class="btn" type="submit"  name ="action" value="cancel">Cancel</button>	
	</span>
</div>
</form>
</div> 
<div id="condition_tpl" style="display:none">
	<div class="condition_info" style="margin-bottom:2px">
			<select class="condition_tyles" name="condition_tyles[]">
				<option value="RULE_CONDITION_LOCATION">location</option>
				<option value="RULE_CONDITION_STOCK">stock</option>
				<option value="RULE_CONDITION_SKU">sku </option>
				<option value="RULE_CONDITION_DATE">date </option>
			</select> 
			<select class="condition_operators" name="condition_operators[]">
				<option value="equal">equal to</option>
				<option value="not_equal">not equal to</option>
				<option value="less_than">less than</option>
				<option value="less_than_equal">less than equal to </option>
				<option value="less_than_equal">greater than</option>
				<option value="greater_than_equal">greater than equal to </option>
				<option value="between">between </option>
				<option value="in">in</option>
				<option value="not_in">not in</option>
			</select>		
			<input class="condition_params" name="condition_params[]" value=""></input>
			<!--<button class="btn add"><span class=""></span></button>-->
			<!--<button class="btn delete"><span class=""></span></button>-->
			<a title="delete this condition" class="btn24 delete" href="#"><span></span></a>
			<!--@import_file-->
	</div>
</div>
<script type="text/javascript">
    var base_url = "{$smarty.const.HOST_ADMIN}";
    {literal}
    $(function(){	
		// init		
		$("#frmRule").delegate("#condition_list a.btn24.delete","click",function(){
			$(this).parent().remove();			
			return false;
		});
		
		$(".condition .add").click(function(){	
			var condition_div = $("#condition_tpl").children('div').clone();
			filterOperator(condition_div, true);
			
			var nextId = $('.condition_info').length;
			var strImport = '<input type="file" id="fileToUpload_' + nextId + '" name="fileToUpload" class="btn import" size="15" value="Import Rule"/>';
			strImport += '<button class="btn" id="buttonUpload_' + nextId + '" onclick="return ajaxUpload(' + nextId + ');" >Upload</button>';
                        var html = condition_div.html().replace('<!--@import_file-->', strImport);
			$("#condition_list").append(html);	
			
			
			return false;
		});
		
		$("#condition_list").delegate("select.condition_tyles", 'change', function(){
			var condition_div = $(this).parent();
			filterOperator(condition_div, true);
		});
		
		$("select#action_type").change(function(){			
			filterActionValue();
		})
		
		$("input#action_rule_stop").change(function(){
			if(this.checked){
				$("#action_text_display").show();
			}
			else{
				$("#action_text_display").hide();
			}
		});
		
    });	
	
	function ajaxUpload(id){
                
		$.ajaxFileUpload({
			url: base_url + '/shippingtime/importrules',
			secureuri:false,
			fileElementId:'fileToUpload_' + id,
			dataType: 'json',
			data:'',
			success: function (data, status)
			{
				$('#buttonUpload_' + id).parent().find('.condition_params').val(data.data.join(','));
				 console.log(data.data.join(','));
			},
			error: function (data, status, e)
			{
				 console.log(data);
			}
		})
		
		return false;
    };  
	
	
	
	function filterActionValue()
	{	
		var action_type = $("select#action_type").val();	
		var curent_value = $("#action_value input").val();
		// init
		$("#action_value").show();	
		$("#action_offset").show();		
		$("#action_value input").datepicker("destroy");
		$("#action_value input").width("30");
		$("#action_value input").val('');
		$("#action_value").next('.unit').text('');
		
		if(action_type == 'RULE_ACTION_GET_LOCATION_TIME')
		{
			$("#action_value").hide();
		}else if(action_type == 'RULE_ACTION_ADD_TIME')
		{			
			$("#action_value").next('.unit').text('(h)');
		}
		else if (action_type == 'RULE_ACTION_SET_TARGET_TIME')
		{		
			$("#action_offset").hide();
			$("#action_value input").datepicker({
				"changeMonth": true,
				"changeYear": true,
				"dateFormat" : "yy-mm-dd",				
			});
			$("#action_value input").datepicker('setDate', curent_value);
			$("#action_value input").width("85");
			//$("#action_value input").datepicker("option","dateFormat","yy-mm-dd");
			$("#action_value").next('.unit').text('(yyyy-mm-dd)');
		}
	}
	
	function filterOperator(condition_div, reset)
	{		
		var conditon_type = condition_div.children('select.condition_tyles').val();		
		var operators_selectbox = condition_div.children('select.condition_operators');
		operators_selectbox.children("option").show();
		switch (conditon_type){
			case 'RULE_CONDITION_LOCATION':			
			case 'RULE_CONDITION_SKU':
			operators_selectbox.children("option[value='less_than']").hide();
			operators_selectbox.children("option[value='less_than_equal']").hide();
			operators_selectbox.children("option[value='less_than_equal']").hide();
			operators_selectbox.children("option[value='greater_than']").hide();
			operators_selectbox.children("option[value='greater_than_equal']").hide();
			operators_selectbox.children("option[value='between']").hide();
			break;
		}
		if(reset == true)
			operators_selectbox[0].selectedIndex = 0;	
		
	}
	
	
    {/literal}
</script>